//
//  CXYLoginViewController.swift
//  诚叙缘
//
//  Created by sujeking on 17/3/20.
//  Copyright © 2017年 szw. All rights reserved.
//

import UIKit

class CXYLoginViewController: CXYBaseViewController {

    
    
    @IBOutlet weak var yhmTextField: UITextField!
    @IBOutlet weak var mmTextField: UITextField!
    
    @IBOutlet weak var signInBtn: UIButton!
    
    var logo : CXYLogoView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    func setupUI() {
        configSignBtn()
        logo = CXYLogoView.init(frame: CGRect(x: 0, y: 0, width: 414, height: 260))
        self.view.addSubview(logo)
    }
    
    func configSignBtn() {
        signInBtn.layer.cornerRadius = signInBtn.bounds.height / 2
        signInBtn.layer.shadowColor = #colorLiteral(red: 0.248970449, green: 0.6003133655, blue: 0.9597421288, alpha: 1).cgColor
        signInBtn.layer.shadowOffset = CGSize(width: 0, height: 3)
        signInBtn.layer.shadowRadius = 5
        signInBtn.layer.shadowOpacity = 0.8
    }
    
    // MARK: - Action
    
    @IBAction func loginBtnClickAction(_ sender: Any) {
//        if self.yhmTextField.text?.characters.count == 0 {
//            return
//        }
        
//        let socket = (UIApplication.shared.delegate as! AppDelegate).socket
//        socket?.delegate = self
//        socket?.login(userName: yhmTextField.text)
//        logo.startAllAnimations(sender: AnyObject.self as AnyObject!)
        let userNameStr = self.yhmTextField.text!
        let passwdStr = self.mmTextField.text!
        
        let dict = ["username":userNameStr, "passwd":passwdStr]
        
        
        CXYNetWork.postMehod(parameters: dict, url: "http://localhost:7777/sign/in") { (dict : Dictionary<String, Any>) in
            if dict["body"] != nil {
                let baseuser:CXYBaseUser = CXYBaseUser.yy_model(with: dict["body"] as! [AnyHashable : Any])!;
                (UIApplication.shared.delegate as! AppDelegate).baseUser = baseuser
                self.dismiss(animated: true, completion: nil)
            } else {
                print("登录失败")
            }
        }
    }
}

extension CXYLoginViewController:CXYSocketDelegate {
    func loginStatus(status: Bool) {
        if status == true {
            self.dismiss(animated: true, completion: nil)
        }
    }
}
